﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.IotInfo.Large;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LJX.SmartPark.Service.AssetManage.EquipmentLedgerServeice;

namespace LJX.SmartPark.Service.IotInfo.Large
{
    public class LargeScreenServeics : ILargeScreenServeics
    {
        private readonly IRepository<LargeScreen> _studentService;

        public LargeScreenServeics(IRepository<LargeScreen> studentService)
        {
            _studentService = studentService;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> DeleteByIdAsync(LargeScreen input)
        {
            var result = await _studentService.DeleteByIdAsync(input.MapTo<LargeScreen>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int GetCount()
        {
            return _studentService.GetCount();
        }

        /// <summary>
        /// 显示分页查询
        /// </summary>
        /// <param name="fenye"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<List<LargeScreen>>> GetLargeScreenAsync(LargeScreenInfo info)
        {
            var students = await _studentService.GetAllListAsync();
            var result = students.MapToList<LargeScreen>();

            if (!string.IsNullOrWhiteSpace(info.localArea))
            {
                result = result.Where(x => x.LocalArea.Contains(info.localArea)).ToList();
            }

            if (!string.IsNullOrWhiteSpace(info.connectionStatus))
            {
                result = result.Where(x => x.ConnectionStatus == bool.Parse(info.connectionStatus)).ToList();
            }

            var totalCount = result.Count();

            result = result.Skip(((info.pageIndex - 1) * info.pageSize)).Take(info.pageSize).ToList();

            return Response<List<LargeScreen>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 根据Id查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Response<LargeScreen> GetLargeScreenById(LargeScreen input)
        {
            var student = _studentService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = student.MapTo<LargeScreen>();
            return Response<LargeScreen>.Ok(result);
        }

        /// <summary>
        /// 添加大屏
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> PostLargeScreen(LargeScreen input)
        {
            var result = await _studentService.InsertAsyncById(input.MapTo<LargeScreen>());

            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 修改大屏
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> PutLargeScreen(LargeScreen input)
        {
            var result = _studentService.UpdateAsync(input.MapTo<LargeScreen>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改状态播放时间
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> PutLargeScreenStation(LargeScreen input)
        {
            input.ConnectionStatus = true;
            input.OpeningTime = DateTime.Now;

            return PutLargeScreen(input);
        }

        /// <summary>
        /// 修改状态关闭时间
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> PutLargeScreenStop(LargeScreen input)
        {
            input.ConnectionStatus = false;
            input.ClosingTime = DateTime.Now;

            return PutLargeScreen(input);
        }
    }

    public class LargeScreenInfo
    {
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
        public string? localArea { get; set; }
        public string? connectionStatus { get; set; }
    }
}
